package com.dragon.mmochat.message;

import java.util.ArrayList;
import java.util.List;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.model.enumType.MmochatOtherSystemSettingType;
import com.dragon.mmochat.model.role.MmochatNpc;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.model.role.MmochatTempNpc;
import com.skymobi.buffer.SkymobiBuffer;
import com.skymobi.message.SkymobiProtocolMessage;

public class Mmochat_2_8S extends SkymobiProtocolMessage {
	int mapId;
	byte line;
	MmochatPlayer me;
	List<Integer> delRoleIds = new ArrayList<Integer>();
	List<MmochatPlayer> addRoles = new ArrayList<MmochatPlayer>();
	List<MmochatPlayer> modifyRoles = new ArrayList<MmochatPlayer>();
	List<Integer> delNpcIds = new ArrayList<Integer>();
	List<MmochatNpc> addNpcs = new ArrayList<MmochatNpc>();

	public Mmochat_2_8S() {
		setMsgCode(MmochatConstant.MSGCODE_MMOCHAT_S_2_8);
	}

	@Override
	protected void encodeBody(SkymobiBuffer out) {
		out.putInt(mapId);
		out.put(line);
		out.put((byte) delRoleIds.size());
		for (Integer roleId : delRoleIds) {
			out.putInt(roleId);
		}
		out.put((byte) addRoles.size());
		for (MmochatPlayer role : addRoles) {
			out.putInt(role.getRoleId());
			out.putUnsignedShort(role.getX());
			out.putUnsignedShort(role.getY());
			out.put((byte) role.getRoleType().getValue());
			out.putUnsignedInt(role.getStateValue(me));
			out.putPrefixedString(role.getName(), 2);

			if (me
					.getOtherSystemSettingByType(MmochatOtherSystemSettingType.不显示他人等级)) {
				if (role.getCurChenHao() == null) {
					out.put((byte) 0);
				} else {
					out.putPrefixedString(role.getCurChenHaoInfo(), 1);
				}
			} else {
				// 显示他人等级
				out.putPrefixedString(role.getLevelInfo(), 1);
			}

		}
		out.put((byte) modifyRoles.size());
		for (MmochatPlayer role : modifyRoles) {
			out.putInt(role.getRoleId());
			out.putUnsignedInt(role.getStateValue(me));
		}
		out.put((byte) delNpcIds.size());
		for (Integer npcId : delNpcIds) {
			out.putInt(npcId);
		}
		out.put((byte) addNpcs.size());
		for (MmochatNpc npc : addNpcs) {
			out.putInt(npc.getNpcId());
			if (npc instanceof MmochatTempNpc) {
				out.put((byte) 1);
			} else {
				out.put((byte) 0);
			}
			out.put((byte) npc.getTaskStateValue(me));
			out.putShort(npc.getX());
			out.putShort(npc.getY());
			out.putUnsignedShort(npc.getBmpType().getValue());
			out.put((byte) npc.getFace().getValue());
			out.putPrefixedString(npc.getName(), 1);
		}
	}

	public void setMapId(int mapId) {
		this.mapId = mapId;
	}

	public void setLine(byte line) {
		this.line = line;
	}

	public void setDelRoleIds(List<Integer> delRoleIds) {
		this.delRoleIds = new ArrayList<Integer>(delRoleIds);
	}

	public void setAddRoles(List<MmochatPlayer> addRoles) {
		this.addRoles = new ArrayList<MmochatPlayer>(addRoles);
	}

	public void setDelNpcIds(List<Integer> delNpcIds) {
		this.delNpcIds = new ArrayList<Integer>(delNpcIds);
	}

	public void setAddNpcs(List<MmochatNpc> addNpcs) {
		this.addNpcs = new ArrayList<MmochatNpc>(addNpcs);
	}

	public void setModifyRoles(List<MmochatPlayer> modifyRoles) {
		this.modifyRoles = new ArrayList<MmochatPlayer>(modifyRoles);
	}

	public void setMe(MmochatPlayer me) {
		this.me = me;
	}

}